package zhihuilan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import zhihuilan.common.utils.RedisCache;
import zhihuilan.entity.MyCircleDiscussThumbup;
import zhihuilan.mapper.MyCircleDiscussThumbupMapper;
import zhihuilan.params.query.MyCircleDiscussThumbupQueryParam;
import zhihuilan.service.MyCircleDiscussThumbupService;

import javax.annotation.Resource;

/**
 * 评论点赞 服务实现
 *
 * @author ruoyi
 */
@Service
public class MyCircleDiscussThumbupServiceImpl extends ServiceImpl<MyCircleDiscussThumbupMapper, MyCircleDiscussThumbup> implements MyCircleDiscussThumbupService {

    @Resource
    private RedisCache redisCache;

    @Override
    public Page<MyCircleDiscussThumbup> selectPageList(MyCircleDiscussThumbupQueryParam queryParam) {
        QueryWrapper<MyCircleDiscussThumbup> queryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(queryParam)) {
            if (!ObjectUtils.isEmpty(queryParam.getStaffId())) {
                queryWrapper.eq("staff_id", queryParam.getStaffId());
            }
        }
        queryWrapper.orderByDesc("create_time");
        long pageNum = 0;
        long pageSize = 10;
        if (queryParam.getPage() >= 0L) {
            pageNum = queryParam.getPage();
        }
        if (queryParam.getPageSize() > 0L) {
            pageSize = queryParam.getPageSize();
        }
        Page<MyCircleDiscussThumbup> page = new Page<>(pageNum, pageSize);
        this.baseMapper.selectPage(page, queryWrapper);
        return page;
    }

    @Override
    public Long selectCount(MyCircleDiscussThumbupQueryParam queryParam) {
        QueryWrapper<MyCircleDiscussThumbup> queryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(queryParam)) {
            if (!ObjectUtils.isEmpty(queryParam.getDid())) {
                queryWrapper.eq("did", queryParam.getDid());
            }
            if (!ObjectUtils.isEmpty(queryParam.getStaffId())) {
                queryWrapper.eq("staff_id", queryParam.getStaffId());
            }
        }
        return this.baseMapper.selectCount(queryWrapper);
    }

    @Override
    public MyCircleDiscussThumbup selectByPidAndStaffId(Integer pid, Integer staffId) {
        QueryWrapper<MyCircleDiscussThumbup> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid", pid);
        queryWrapper.eq("staff_id", staffId);
        return this.baseMapper.selectOne(queryWrapper);
    }

    @Override
    public MyCircleDiscussThumbup addEntity(MyCircleDiscussThumbup myCircleThumbup) {
        this.baseMapper.insert(myCircleThumbup);
        return myCircleThumbup;
    }

    @Override
    public Boolean deleteByPidDidAndStaffId(Integer pid, Integer did, Integer staffId) {
        QueryWrapper<MyCircleDiscussThumbup> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid", pid);
        queryWrapper.eq("did", did);
        queryWrapper.eq("staff_id", staffId);
        int count = this.baseMapper.delete(queryWrapper);
        return count > 0 ? true : false;

    }
}
